<template>
  <div class="app-container">
    <el-card class="content">
      <el-row>
        <el-col :span="100">
          <el-form :model="userInfo" label-width="100px">
            <el-form-item label="旧密码：">
              <el-input ref="oldPassword" v-model="userInfo.oldPassword" type="password" class="input-width" />
            </el-form-item>
            <el-form-item label="新密码：">
              <el-input ref="newPassword" v-model="userInfo.newPassword" type="password" class="input-width" />
            </el-form-item>
            <el-form-item label="确认新密码：">
              <el-input ref="newPasswordConfirm" v-model="userInfo.newPasswordConfirm" type="password" class="input-width" />
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <div class="operate-container">
        <el-button class="submit-button" type="primary" size="medium" :round="true" @click="submit">保存</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>

export default {
  data() {
    return {
      userInfo: {
        oldPassword: '',
        newPassword: '',
        newPasswordConfirm: ''
      }
    }
  },
  computed: {},
  created() {
  },
  methods: {
    submit() {
      if (this.userInfo.oldPassword === '') {
        this.$nextTick(() => this.$refs.oldPassword.focus())
        return this.$commonUtils.showErrorToast('请输入旧密码')
      }
      if (this.userInfo.newPassword === '') {
        this.$nextTick(() => this.$refs.newPassword.focus())
        return this.$commonUtils.showErrorToast('请输入新密码')
      }
      if (this.userInfo.newPasswordConfirm === '') {
        this.$nextTick(() => this.$refs.newPasswordConfirm.focus())
        return this.$commonUtils.showErrorToast('请输入确认新密码')
      }
      if (this.userInfo.newPassword !== this.userInfo.newPasswordConfirm) {
        this.$nextTick(() => this.$refs.newPasswordConfirm.select())
        return this.$commonUtils.showErrorToast('确认新密码与新密码不一致')
      }
      this.$commonUtils.showLoading()
      const params = {
        verifyType: 'account',
        oldPassword: this.userInfo.oldPassword,
        loginPassword: this.userInfo.newPassword
      }
      this.$api.updatePassword(params).then(res => {
        this.$commonUtils.hideLoading()
        if (res.code === 0) {
          this.$alert('修改成功', '提示', {
            confirmButtonText: '确定',
            type: 'success'
          }).then(() => {
            window.parent.postMessage({
              cmd: 'closeTabItem',
              params: ''
            })
          })
        }
      }, _ => {
        this.$commonUtils.hideLoading()
        this.$nextTick(() => this.$refs.oldPassword.focus())
      })
    }
  }
}
</script>

<style lang="scss" scoped>

  .content {
    display: flex;
    justify-content: center;
  }

  .operate-container {
    margin-top: 40px;
    text-align: center;
    padding-right: 20px;
  }

  .submit-button {
    width: 200px;
  }
</style>
